<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewMember extends JView
{
	function display($tpl=null) {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		if( $this->getLayout() == 'form' ) {
			$this->_displayForm($tpl);
			return;
		}
		
		if( $this->getLayout() == 'account' ) {
			$this->_displayReceiverAccount($tpl);
			return;
		}

		if( $this->getLayout() == 'formaccount' ) {
			$this->_displayFormReceiverAccount($tpl);
			return;
		}

		if( $this->getLayout() == 'detailaccount' ) {
			$this->_displayDetailReceiverAccount($tpl);
			return;
		}
		
		if( $this->getLayout() == 'transfer' )
		{
			$this->_displayTransfer($tpl);
			return;
		}

		if($user->gid < 25){
			$canView		= Ownservice::checkAccess('member', 'view', 'member', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/icon.css');
		$document->addScriptDeclaration(
									"
										window.addEvent('domready', function() {
											SqueezeBox.initialize();
											$$('a.img-preview').each(function(el) {
												el.addEvent('click', function(e) {
													new Event(e).stop();
													SqueezeBox.fromElement(el);
												});
											});
										});
									");

		// add bootstrap to document
		$document->addStyleSheet(JURI::base(true).'/components/com_ownservice/assets/bootstrap-3.1.1/css/bootstrap.min.css');

		// add script to document
		$document->addScript(JURI::base(true).'/components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		$document->addScript(JURI::base(true).'/components/com_ownservice/assets/bootstrap-3.1.1/js/bootstrap.min.js');

		//Get data form the model
		$model		= $this->getModel();
		$row		= $this->get('Data');
		$files		= $model->getFiles($row->user_id);

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canEdit	= ($user->gid < 25)? Ownservice::checkAccess('member','edit','member','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_PERSONAL_DETAIL'));
		$document->setTitle( $params->get('page_title') );

		JHTML::_('behavior.modal', 'a.modal');

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('files', $files);
		$this->assignRef('access', $access);

		parent::display($tpl);
	}

	function _displayForm() {
		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$gid		= $user->get('id');
		$acl		= & JFactory::getACL();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/icon.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');
		//add js autocomplete
		$document->addScript('components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		$document->addScriptDeclaration(
									"
										function addUploadFile() {
											var li 		= document.createElement('li');
											var file	= document.createElement('input');
											var buttondel = document.createElement('input');
											var buttonadd = document.createElement('input');

											buttondel.type	= 'button';
											buttondel.name	= 'removebutton';
											buttondel.id	= 'removebutton';
											buttondel.style.border = '1px';
											buttondel.style.borderStyle='solid';
											buttondel.style.borderColor='silver';
											$(buttondel).addEvent('click', function() { deleteFile(this) });
											buttondel.value = '".JText::_( 'OWNSERVICE_REMOVE_FILE' )."';

											buttonadd.type = 'button';
											buttonadd.name	= 'addbutton';
											buttonadd.id	= 'addbutton';
											buttonadd.style.border = '1px';
											buttonadd.style.borderStyle='solid';
											buttonadd.style.borderColor='silver';
											$(buttonadd).addEvent('click', function() { addUploadFile() });
											buttonadd.value = '".JText::_( 'OWNSERVICE_ADD_FILE' )."';

											file.type	= 'file';
											file.name	= 'Filedata[]';

											var filelist = document.getElementById('addfile');

											filelist.appendChild(li);
											li.appendChild(file);
											//li.appendChild(buttonadd);
											li.appendChild(buttondel);
										}

										function deleteFile(el) {
											var file	= $(el);
											var row		= file.getParent();
											var fx		= row.effects({duration: 300, transition: Fx.Transitions.linear});
											
											fx.start({
												'height': 0,
												'opacity': 0			
											}).chain(function(){
												row.remove();
											});
										}

										window.addEvent('domready', function() {
											SqueezeBox.initialize();
											$$('a.img-preview').each(function(el) {
												el.addEvent('click', function(e) {
													new Event(e).stop();
													SqueezeBox.fromElement(el);
												});
											});

											$('issuer').addEvent('change', function(event){
												var value = this.value;
												if(value == 'Other') {
													$('issuerother').setProperty('style', 'display:block');
												} else {
													$('issuerother').setProperty('style', 'display:none');
												}
											});
										});
										
										jQuery(document).ready(function() {
											jQuery('#branch_id').change(function() {
												var branchid= this.value;
											  	jQuery.post(\"index.php?option=com_ownservice&controller=member&task=getCurrency&".JUtility::getToken()."=1\", { branch_id:branchid},
												function(data){
													jQuery('#currencyrate').html(data);
												});
											});
										});
									"
								);

		//Get data form the model
		$model		= $this->getModel();
		$row		= & $this->get('Data');
		$branch_id	= $row->branch_id?$row->branch_id:$ownuser->get('branch_id');
		$currencies	= $model->getCurrencies($branch_id);
		$countries	= & $this->get('Countries');
		$files		= $model->getFiles($row->user_id);

		// check if it's an edit action
		if($row->id) {
			$canEdit	= ($gid<25)?Ownservice::checkAccess('member', 'edit', 'member', 'all'):1;
			if (!$canEdit) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		} else {
			$canAdd		= ($gid<25)?Ownservice::checkAccess('member', 'add', 'member', 'all'):1;
			if (!$canAdd) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canChangeGroup		= ($user->gid < 25)? Ownservice::checkAccess('member','changegroup','member','all'):1;

		//set page title
		if($row->id) { 
			$params->set('page_title',	JText::_('OWNSERVICE_EDIT_MEMBER'));
		} else { 
			$params->set('page_title',	JText::_('OWNSERVICE_NEW_MEMBER'));
		} 
		$document->setTitle( $params->get('page_title') );

		$lists = array();

		if($access->canChangeGroup) {

			$groups = $this->get('Groups');
			//build ownservices groups select list
			$lists['group'] = OwnserviceHelper::buildgroupsselect($groups, 'group_id',$row->group_id, true, 'class="inputbox" style="width:250px;"');

			$branches = $this->get('Branches');
			//build branch select list
			$lists['branch'] = OwnserviceHelper::buildbranchesselect($branches,'branch_id', $row->branch_id, true, 'class="inputbox" style="width:250px;"');
			
		}

		//build currency exchange list
		$lists['currency'] = OwnserviceHelper::buildcurrenciesselect($currencies, 'rate_id', $row->rate_id, true, 'class="inputbox" style="width:250px;"');

		//build country select list
		$lists['country'] = OwnserviceHelper::buildcountryselect($countries, 'country',$row->country,true, 'class="inputbox" style="width:250px;"');

		//build id type select list
		$lists['id_type'] = OwnserviceHelper::buildidtypeselect('id_type', $row->id_type, true, 'class="inputbox" style="width:250px;"');
		
		//build issuer select list
		$lists['issuer'] = OwnserviceHelper::buildissuerselect('issuer', $row->issuer, true, 'class="inputbox" style="width:250px;"');

		JHTML::_('behavior.modal', 'a.modal');

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('lists', $lists);
		$this->assignRef('access', $access);
		$this->assignRef('files', $files);

		parent::display($tpl);
	}

	function _displayReceiverAccount($tpl){

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/icon.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		if($user->gid < 25){
			$canView		= Ownservice::checkAccess('account', 'view', 'account', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}

		//Get data form the model
		$model		= $this->getModel();
		$member		= $this->get('Data');
		$rows		= $this->get('Accounts');

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canView		= ($user->gid < 25)? Ownservice::checkAccess('account','view','account','all'):1;
		$access->canEdit		= ($user->gid < 25)? Ownservice::checkAccess('account','edit','account','all'):1;
		$access->canAdd			= ($user->gid < 25)? Ownservice::checkAccess('account','add','account','all'):1;
		$access->canDelete		= ($user->gid < 25)? Ownservice::checkAccess('account','delete','account','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_ACCOUNTS'));
		$document->setTitle( $params->get('page_title') );

		$this->assignRef('member', $member);
		$this->assignRef('rows', $rows);
		$this->assignRef('access', $access);

		parent::display($tpl);
	}

	function _displayFormReceiverAccount($tpl) {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		$gid = $user->gid;

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/icon.css');
		//add js autocomplete
		$document->addScript(JURI::base(true).'/components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		$document->addScript(JURI::base(true).'/components/com_ownservice/assets/js/ownservice.js');

		$document->addScriptDeclaration(
			'
				window.addEvent("domready", function() {
					$("bank_name").addEvent("change", function(event){
						var value = this.value;
						if(value == "other") {
							$("bankother").setProperty("style", "display:block");
						} else {
							$("bankother").setProperty("style", "display:none");
						}
					});
					
					$("account_type").addEvent("change", function(event){
						var value = this.value;
						if(value == "other") {
							$("typeother").setProperty("style", "display:block");
						} else {
							$("typeother").setProperty("style", "display:none");
						}
					});
				});
				
				jQuery(document).ready(function () {
					jQuery("#account_no").on("keyup focus", function() {
						var account		= jQuery("#account_no").val();
						var num			= account.length;
						if(num < 10){
							jQuery("#save").attr("disabled", "disabled");
							jQuery("#shownum").css("color","red");
							jQuery("#shownum").css("font-weight", "bold");
						} else {
							jQuery("#save").removeAttr("disabled");
							jQuery("#shownum").css("color","black");
							jQuery("#shownum").css("font-weight", "normal");
						}
						jQuery("#shownum").text((num));
					});
				});
			');

		//Get data form the model
		$model		= $this->getModel();
		$member		= $this->get('Data');
		$row		= $this->get('Account');
		$countries	= $this->get('Countries');

		// check if it's an edit action
		if($row->id) {
				$canEdit	= ($gid<25)?Ownservice::checkAccess('account', 'edit', 'account', 'all'):1;
				if (!$canEdit) {
					//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect( 'index.php?option=com_ownservice');
				}
		} else {
				$canAdd		= ($gid<25)?Ownservice::checkAccess('account', 'add', 'account', 'all'):1;
				if (!$canAdd) {
					//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
					JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
					$mainframe->redirect( 'index.php?option=com_ownservice');
				}
		}

		//set page title
		if($row->id) {
			$params->set('page_title',	JText::_('OWNSERVICE_EDIT_ACCOUNT'));
		} else {	
			$params->set('page_title',	JText::_('OWNSERVICE_NEW_ACCOUNT'));
		}
		$document->setTitle( $params->get('page_title') );

		$lists = array();

		//build account type select list
		$lists['type'] = OwnserviceHelper::buildaccounttypeselect('account_type',$row->account_type, true, 'class="inputbox" style="width: 250px;"');

		//build country select list
		$lists['country'] = OwnserviceHelper::buildcountryselect($countries, 'country',($row->country?$row->country:'Thailand'),true, 'class="inputbox" style="width: 250px;"');

		//build banks to select list
		$lists['bank'] = OwnserviceHelper::buildbanknamelist('bank_name', trim($row->bank_name), true, 'class="inputbox" style="width: 250px;"');

		$this->assignRef('member', $member);
		$this->assignRef('row', $row);
		$this->assignRef('lists', $lists);

		parent::display($tpl);
	}

	function _displayDetailReceiverAccount($tpl) {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		if($user->gid < 25){
			$canView		= Ownservice::checkAccess('account', 'view', 'account', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$member		= $this->get('Data');
		$row		= $this->get('Account');

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_ACCOUNT_DETAIL'));
		$document->setTitle( $params->get('page_title') );

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canEdit	= ($user->gid < 25)? Ownservice::checkAccess('account','edit','account','all'):1;

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('member', $member);
		$this->assignRef('access', $access);

		parent::display($tpl);
	}

	function _displayTransfer($tpl)
	{
		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$db			= & JFactory::getDBO();
		$params 	= & $mainframe->getParams('com_ownservice');

		$nullDate		= $db->getNullDate();

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/icon.css');
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		if($user->gid < 25){
			$canView		= Ownservice::checkAccess('transaction', 'view', 'transaction', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}

		//Get data form the model
		$model		= $this->getModel();
		$member	= $this->get('Data');
		$rows			= $this->get('Transfer');
		
		//assign data from the model
		$this->assignRef('member', $member);
		$this->assignRef('nullDate', $nullDate);
		$this->assignRef('rows', $rows);
		
		parent::display($tpl);
	}
}